const api = require('../../utils/api.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: '',
    couponList: [],
    page: 1,
    couponDiscount: '', //优惠券折扣
    total: '', // 优惠券张
    end: false,
    payAmount: '', // 订单总金额
    activeIndex: '0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userId: options.userId,
      payAmount: options.payAmount || '',
      couponList: []

    })
    this.getCouponList('0');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // var scroll_page = this.data.page;
    // if (!this.data.end) {
    //   scroll_page++;
    //   this.setData({
    //     page: scroll_page
    //   })
    //   this.getCouponList(this.data.activeIndex, scroll_page, )
    // } else {
    //   return wx.showToast({
    //     title: '没有更多优惠券了',
    //     icon: 'none',
    //     duration: 1500
    // })
      
    // }
  },

   /* 获取优惠券 */
   getCouponList: function () {
    wx.stopPullDownRefresh()
    var that = this;
    var lists = this.data.couponList;
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function () {
      api.post('operation/activity/getRegisterActivty', {
       
        shopId: app.globalData.shopId + '',
      }).then(res => {
        if (res.data.success && res.data.data.length) {
          res.data.data.map(item => {
            var coupon_item = {};
            coupon_item.title = item.disName;
            coupon_item.date = item.limitDay;
            coupon_item.count = item.num/100;
            coupon_item.limitUsed = item.limitTime;
            coupon_item.id = item.id;
            coupon_item.disType = '01';
            coupon_item.unUsableMsg =item.unUsableMsg
            if(typeof(item.maxAmount)==="number"){
              coupon_item.maxAmount = item.maxAmount/100;
            }
            coupon_item.minAmount = item.minAmount/100;
            lists.push(coupon_item)
          })
          that.setData({
            couponList: lists,
            total:  res.data.data.total
          })
          wx.hideLoading()
        }

        // if(that.data.page*10 >= res.data.data.total){
        //   that.setData({
        //     end: true
        //   })
        // }
       
      })

    }, 500)

  },
  /* 不使用优惠券 */
  clearCoupon:function(){
    wx.setStorageSync('couponDiscount', '');
    wx.navigateBack();
  },
  /* 保存按钮 */
  gotoPay: function() {
    wx.navigateBack();
  },
   /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.setData({
    //   couponList: [],
    //   page: 1,
    //   end: false
    // })
    // this.getCouponList(this.data.activeIndex, 1);
  },
   /* 手机号授权并注册登录 */
   getPhoneNumber: function (e) {
    var that = this;
    wx.showLoading({
        title: '加载中',
    })
    api.post('user/decrypt', {
        sessionKey: encodeURIComponent(wx.getStorageSync('user').session_key),
        encryptedData: encodeURIComponent(e.detail.encryptedData),
        iv: encodeURIComponent(e.detail.iv)
    }).then(res => {
        if (res.data.success) {

            that.setData({
                phone: res.data.data.phoneNumber
            })
            wx.setStorageSync('phone', res.data.data.phoneNumber);
            // 注册登录 
            api.post('user/getByMobile', {
                mobile: res.data.data.phoneNumber || wx.getStorageSync('phone'),
                openid: wx.getStorageSync('user').openid,
                shopId: app.globalData.shopId,
                // unionid: wx.getStorageSync('unionId')
            }).then(response => {
               
                // 获取用户信息
                if (response.data.success) {
                  wx.redirectTo({
                    url: '/pages/user/index'
                  })
                }
                // 手机号激活后没有会员直接跳转领卡
                if (!response.data.data.wxCard) {
                    // that.clickMyCard()
                    // that.openCard()
                }

            })
            wx.hideLoading()



        }
        wx.hideLoading()
    })


},
})